<?php
	/**
	* Ads_assign_model 
	* 
	* Ads_assign_model manage the business methods for ads_assign table 
	* 
	* 
	* @author Thang
	* @version $Id:  ads_assign_model.php 2012-04-13
	* @access public
	*
	*/
	class Ads_assign_model extends CI_Model{
		
		public function __construct(){
			$this->load->database();
		}
		
		
		/**
		* getAdsAssign(): 	get ads assign data, joining with kiosk and ads 
		*					
		* 
		* @param 			int - the id of ads assign
		*
		* @return    		array - record of ads_assign
		* @access public
		*/
		public function getAdsAssign($id){
			if($id!=FALSE){
				$this->db->select('s.*, a.ad_text, k.mac_address');
				$this->db->from('ads_assign s');
				$this->db->join('ads a', 'a.id = s.ads_id', 'left');	
				$this->db->join('kiosk k', 'k.id = s.kiosk_id', 'left');	
				$this->db->where('s.id', $id);
				$query = $this->db->get();				
				return $query->row_array();
			}
			else return false;
		}
		
		/**
		* getAdsAssignList(): return data from ads_assign table join with ads table
		* 
		* @param     int $id is the ads_id of ads_assign table
		* @return    array
		*
		*/
		public function getAdsAssignList($id=FALSE){
			$this->db->select('s.*, a.ad_text, k.mac_address');
			$this->db->from('ads_assign s');
			$this->db->join('ads a', 'a.id = s.ads_id', 'left');
			$this->db->join('kiosk k', 'k.id = s.kiosk_id', 'left');
			
			if($id!=FALSE){				
				$this->db->where('s.ads_id = '.$id, NULL, FALSE);				
				$query = $this->db->get();	
				return $query->result_array();
			}
			else{				
				$query = $this->db->get();	
				return $query->result_array();
			}
		}
		
		
		/**
		* getAdsAssignListGroup(): return data from ads_assign table
		* 
		* @param     int $id is the ads_id of ads_assign table
		* @return    array
		*
		*/
		public function getAdsAssignListGroup($id=false){
			if($id === FALSE){
				$query = $this->db->get('ads_assign');
				return $query->result_array();
			}
			$this->db->select('s.*, a.ad_text, k.mac_address');
			$this->db->from('ads_assign s');
			$this->db->join('ads a', 'a.id = s.ads_id', 'left');
			$this->db->join('kiosk k', 'k.id = s.kiosk_id', 'left');
			$this->db->where('s.ads_id',$id);
			$query = $this->db->get();	
			return $query->result_array();
		}
		
		
		/**
		* save(): store the date to ads_assign table
		* 
		* @param
		* @return    
		* @access public
		*/
		public function save(){
			$id = $this->input->post('id');
			
			// get Start date string from input form
			$start_str 	= $this->input->post('start_date') ." ". $this->input->post('start_h').":".$this->input->post('start_i').":".$this->input->post('start_s');
			
			// get End date string from input form
			$end_str 	= $this->input->post('end_date') ." ". $this->input->post('end_h').":".$this->input->post('end_i').":".$this->input->post('end_s');
			
			// convert start date and end date string to sql format
			$start_date = date('Y-m-d h:i:s', strtotime($start_str));
			$end_date 	= date('Y-m-d h:i:s', strtotime($end_str));
			
			// get the list kiosk as array
			$arr_kiosk = $this->input->post('kiosk_id');
									
			// loop the $arr_kiosk array list, each index make the insert or update record
			foreach($arr_kiosk as $v){
				$data = array(
					"kiosk_id" 	=> $v,
					"ads_id" 	=> $this->input->post('ads_id'),
					"status" 	=> $this->input->post('status'),
					"start_date" => $start_date,
					"end_date" 	=> $end_date,
				);
				
				if(empty($id))
					$this->db->insert('ads_assign', $data);
				else{
					$this->db->where('id', $id);
					$this->db->update('ads_assign', $data);
				}
			}			
		}		
		
		/**
		* delete(): remove the ads_assign record
		* 
		* @param int $id is the id for remove condition
		* @return    
		*
		*/
		public function delete($id){
			$this->db->where('id', $id);
			$this->db->delete('ads_assign');
		}
		
		public function get_assign_text($id){
			$this->db->select('s.*, a.ad_text, k.mac_address, k.name');
			$this->db->from('ads_assign s');
			$this->db->join('ads a', 'a.id = s.ads_id', 'left');
			$this->db->join('kiosk k', 'k.id = s.kiosk_id', 'left');
			$this->db->where('s.kiosk_id = '.$id, NULL, FALSE);
			$query = $this->db->get();	
			return $query->row_array();
		}
	}
?>